;
(function() {
  new Vue({
    el: '#BT_right_header',
    data: {
      TemplateName: '',
      systemContent: '',
      link: '',
      copyBtn: null, //存储初始化复制按钮事件
      permissions: JSON.parse(window.localStorage.getItem('permissions')),
      fileDetail: '',
      setfieldData: '',
      keyword: '',
      copyInputText: '',
			uploadsrc: uploadUrl,
			tmpData: []
    },
    methods: {
			inputFilter: function (newFile, oldFile, prevent) {
				if (newFile && !oldFile) {
					// 过滤不是图片后缀的文件
					if (!/\.(docx)$/i.test(newFile.name)) {
						hl.toast('请上传docx格式的文件')
						return prevent()
					}
				}				
			},
			inputFile: function (newFile, oldFile) {			
				if (!newFile) newFile = {active: true}
				newFile.active = true
			  if (newFile && oldFile && newFile.active && oldFile.active) {					
			    // 获得相应数据
			    if (newFile.xhr) {						
						if (newFile.xhr.status == '500') {
							var msg = JSON.parse(newFile.xhr.response)
							hl.toast(msg.msg)
						}
			      //  获得响应状态码
						if (newFile.response.url) {
							newFile.active = false
						}
			    }
			  }				
			},	
      // 获取系统名称
      getSystemConfigItem() {
        var that = this;
        hlhttp.get({
          url: 'contract/print_template_field',
          data: {
            key: this.keyword || '#',
          },
          success: function(data) {
            data.forEach((item, index) => {
              item.nickname = ''
            });
            data.forEach((item, index) => {
              item.showName = '${' + item.fieldName + '}'
              if (item.nickname) {
                item.isShow = true
                item.isEdit = false
              } else {
                item.isShow = false
                item.isEdit = false
              }
            });
            that.systemContent = data
          }
        })
      },
      // 失去焦点事件
      blurTi(index) {
        if (!this.systemContent[index].nickname) {
          this.systemContent[index].isShow = false
        }
        this.systemContent[index].isEdit = false
      },
      //点击复制文本
      copyText(index,type) {
        // var btn = $('.copyBtn');
        // var clipboard = new Clipboard(btn); //实例化
     
        if(type == '系统内置字段'){
          this.copyInputText = this.systemContent[index].showName
        }else{
          this.copyInputText = this.systemContent[index].templateKey
        }
        var Url = document.getElementById("copyInput");
        Url.value = this.copyInputText
        Url.select(); // 选择对象
        document.execCommand("Copy"); // 执行浏览器复制命令
        hl.toast('复制成功')
      },      
      //预览
      goToPreview() {
        hlUrlUtil.goTo('billTemplate_preview', {
          ddtab: true
        }, true)
      },
      // 保存模板 < == > 服务器
      sendSave(cb) {
        if (!this.TemplateName) {
          hl.toast('请输入模板名称')
          return
        }
        if (!this.tmpData.length) {
          hl.toast('请上传合同模板')
          return
        }

        let arr = []
        this.systemContent.forEach(item => {
          let single = {
            field_name: item.fieldName,
            custom_field_name: item.nickname || '#',
          }
          arr.push(single)
        })
        this.setfieldData = arr

        var that = this
        hlhttp.post({
          url: 'contract/print_template',
          data: {
            template_name: that.TemplateName,
            template_fields: JSON.stringify(that.setfieldData),
            attachment_name: that.tmpData.length ? that.tmpData[0].response.name : '#',
            attachment_url: that.tmpData.length ? that.tmpData[0].response.url : '#',
          },
          success: function(data) {
            if (cb) cb(data)
          }
        })
      },
      // 保存模板
      saveTemplate(cb) {
        this.sendSave((data) => {
          hl.toast('合同模版保存成功');
          hlUrlUtil.goTo('templateConfig', {
            defaultName: 'bill',
            ddtab: true
          }, false)
        })
      },
      //搜索关键字
      searchKey() {
        this.getSystemConfigItem();
      },
      // 重置
      reset() {
        this.keyword = ''
        this.getSystemConfigItem();
      }

    },
    mounted() {
      this.getSystemConfigItem();      
    },
  })
}())
